package com.stark.wechat.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.stark.wechat.context.ScopeUtil;

/**
 * 作用域拦截器。
 * <p>把 {@link HttpServletRequest} 和 {@link HttpServletResponse} 绑定到 {@link ScopeUtil} 。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.0, 2017/8/5
 */
public class ScopeInterceptor extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
			// 绑定到LocalThread
			ScopeUtil.setRequest(request);
			ScopeUtil.setResponse(response);
		}
		
        return true;
	}
}